package com.ruoyi.common.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

import javax.crypto.Cipher;
import java.security.KeyFactory;
import java.security.PrivateKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.util.Base64;
public class RSADecrypt {
    public static void main(String[] args) throws Exception {




        RestTemplate client = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        JSONObject json = new JSONObject();
        json.put("loginToken", "9GZwxclBg7vXOsOEogTcpR5/Ng8N169exDZlbBGS6jmvpKQkx8JvSs+lGVaxVRF0WBup2pBzrEfOpNkaVE0w32Vw8xVLUTUIxaJL3Um0e7yxWQlf6YHlY5KHi3ICMWoxbnEfAdMzJUywmxJK6bijsMfzQPQJXXl1CbEljtV7k1vpRljv/GnJXgxeSjdZfH28YIIi26HJzHq+3ZlN+Kh7EbTxZ6ViV0opu1ooWzemegL2Po5DqB0jhBY5Fuo+BI1ruqVKIAVS/e+AStlw8L0j7+qZ9YgzanQkrYlX2yjIbplM3a52cV4s2sz7FNdum6qg+1XImXGZmOo5l7q8DnMJ0g==");
        MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
        headers.setContentType(type);
        headers.add("Accept", MediaType.APPLICATION_JSON.toString());
        headers.add("Authorization","Basic "+Base64.getUrlEncoder()
                .encodeToString(("0f950dfa5f8ab4e53971da9f"+ ":" + "18ad62a776a30df068c65225").getBytes()));
        // 以表单的方式提交
        //将请求头部和参数合成一个请求
        HttpEntity<String> requestEntity = new HttpEntity<>(json.toString(), headers);
        //执行HTTP请求，将返回的结构使用ResultVO类格式化
        ResponseEntity<String> response = client.postForEntity("https://api.verification.jpush.cn/v1/web/loginTokenVerify", requestEntity, String.class);
        System.out.println(response.getBody());
//
        JSONObject jsonObject = JSON.parseObject(response.getBody());
        if (jsonObject.getIntValue("code") != 8000){
            return;
        }

        String phone = decrypt("UHh5f5aMMcS+2GFJfYXlRx2kyitQ0RZom+CJWEtXTnHZPCDiIQuysTdoyugbke9bNDfBDfDXuokeiitoh5Mgbi9OzP9oN4k/a1sejbpGqGT4VHlQJZ71sUgA/jqBPyhXDXXcVXholjUGVE4H1dkzs6t1CNPzjUMU13WA/+Pyi70=",
                "MIICdQIBADANBgkqhkiG9w0BAQEFAASCAl8wggJbAgEAAoGBAK9ahutIXhGwY+6U" +
                        "O4hFC31xwaCZuGnIozgcGjN9cCN6FxF7Exos+QDy9+N/J/uo7zmoBLO7uVlPUjrF" +
                        "DcYnXTfEzBasBi3/uAqH2dpwWBrNB2GZ5AbMrJYqs62z0fwlQBcEUFIbWLpRTKOv" +
                        "IeGXX6l6JnyixoujH0Y4JhABrVCnAgMBAAECgYAke34kh2ri3UL9SSPVpx/L2pYB" +
                        "VtoSUCb9LoOZANkK2ITiIywHPyITO3mtgDAOYMhcw4Mu7biTRziBd6v09Q2ki3Xy" +
                        "tao5itu688gIBuEi3W5eIzuWFMsYu5Cm+kihAqD7UzQl2AdwQ78Oocx69gFLIwrR" +
                        "6BGgf/n1fvdVxew5KQJBANyvNZ2RXZvJq3CAx0IEbmcYWgY8XxGGndcY6yRdyANb" +
                        "d6YXl7jG+8Z1sc17ZjSUsw6LIYw3TDmA0dizi/Bnwz0CQQDLaj+ibMsJsT4v+zij" +
                        "oZ03l5Uv2d98ffp0XCwKa4/AJkLz9rt4flNB34Scx95qgTEuMab/lmPwFBKJYCPN" +
                        "V9GzAkAvzXQbsYVJXaHZOMvwrO0NP+UgPKuLLFG8ahVlRzswHvwH6kxfJhsbnhrp" +
                        "m7VD5oHywIh/yuqj+hd6jUnexVsxAkAPK+WDH8ok9utEeOBIlA090Dh4US+yAVNk" +
                        "ylsei9c05aWovIZO/l0HVLx3VRDpsKkSYPGrrnPVvCmFxX3mGt6xAkBG6GJ5YgVS" +
                        "c7YZ+I6g08gqCa4R2EoCqJn/Mf3X3/mZXLNtViRUY4DeAKYELrPGrMKNvU3fCUgv" +
                        "WQVbCThhT3st");
        System.out.println(phone);
    }

    public static String decrypt(String cryptograph, String prikey) throws Exception {
        PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(Base64.getDecoder().decode(prikey));
        PrivateKey privateKey = KeyFactory.getInstance("RSA").generatePrivate(keySpec);

        Cipher cipher=Cipher.getInstance("RSA");
        cipher.init(Cipher.DECRYPT_MODE, privateKey);

        byte [] b = Base64.getDecoder().decode(cryptograph);
        return new String(cipher.doFinal(b));
    }





}
